package com.dayuanit.dy15.atm.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MD5Utils {
    //username | passowrd | 盐值
    private static final String userPwdPlaintext = "%s|%s|%s";
    private static final String emailActivePlaintext = "%s|%s|%s";

    @Value("${user.password.salt}")
    private String userPwdSalt;

    @Value("${email.salt}")
    private String emailSalt;

    public String userPassword(String password, String username) {
        String plaintext = String.format(userPwdPlaintext, username, password, userPwdSalt);
        return DigestUtils.md5Hex(plaintext);
    }

    // //混淆明文 明文的组成=email + 随机的字符串(8位的) + 过期时间(毫秒数)
    public String emailActiveUrl(String email, long expireTime) {
        String plaintext = String.format(emailActivePlaintext, email, emailSalt, expireTime);
        return DigestUtils.md5Hex(plaintext);
    }
}
